#!/usr/bin/env bash

# xb - a wannabe cross system source builder written in bash
# by Adnan Shameem (adnan360); License: MIT (Expat)

cd "$(dirname $0)" && source "inc/common.sh"

install_packages_build=''
install_packages_deps=''
declare -a xb_filenames

if [ -d '/usr/local/etc' ]; then
	usrdir='/usr/local'
	etcdir='/usr/local/etc'
else
	usrdir='/usr'
	etcdir='/etc'
fi

# Stores where sources will be downloaded and built
xb_parent_dir="$HOME/.cache/xb"

for param in "$@"; do

	if [ -f "$param" ]; then
		xb_filename="$param"
	elif [ -f "$(dirname $(realpath $0))/xbrepo/${param}.xb" ]; then
		xb_filename="$(dirname $(realpath $0))/xbrepo/${param}.xb"
	else
		echo "Error: Parameter $param is invalid"
		continue # no file found, so look for next params
	fi

	if [ ! -f "$xb_filename" ]; then
		echo "Error: File $xb_filename not found"
		continue # go to next param
	fi
	xb_filenames+=("$xb_filename")

	echo "Reading info from ${xb_filename}"

	# Clean previous memory
	unset build_depends
	unset depends
	unset sources

	source "$xb_filename"

	if [[ "$sources" =~ .*".git".* ]] && [ "$(is_binary_installed "git")" = "$FALSE" ]; then
		install_packages_build+=' git'
	fi
	# Bring in deps from xb file
	install_packages_build+="$build_depends"
	install_packages_deps+="$depends"

done

## Step 1: Install deps
# Prepare list
install_packages_build=$(convert_package_names "$install_packages_build" "$TRUE")
install_packages_deps=$(convert_package_names "$install_packages_deps" "$TRUE")
# Actually install
echo "Will be installing packages: ${install_packages_build} ${install_packages_deps}"
install_packages "${install_packages_build} ${install_packages_deps}"
if [ -d /usr/local/share/icons ]; then
	system_icon_path='/usr/local/share/icons'
else
	system_icon_path='/usr/share/icons'
fi

for xb_filename in "${xb_filenames[@]}"; do
	builddir="${xb_parent_dir}/$(random_string)"
	# Some distros, like in FreeBSD, GNU Make is available as "gmake". On others simply "make".
	gnumakebin=$(if [ "$(is_binary_installed "gmake")" = "$TRUE" ]; then echo gmake; elif [ "$(is_binary_installed "make")" = "$TRUE" ]; then echo make; fi)
	gnumakeparams=''
	mkdir -p "${builddir}"
	echo "Created $builddir for the build"

	# Unset previously set values
	unset do_download
	unset do_build
	unset do_install
	unset build_depends
	unset depends
	unset sources
	unset tested_systems
	
	source "$xb_filename"
	echo "Starting building xb file: ${xb_filename}"

	## Step 2: Download sources
	cd "${builddir}"
	for src in $sources; do
		if [[ "$src" =~ .*".git".* ]]; then
			git clone --depth 1 "$src"
			echo "Source '${src}' cloned in ${builddir}"
		else
			curl -LO "$src"
			echo "Source '${src}' downloaded in ${builddir}"
		fi
	done

	## Step 3: Download function
	[ -n "$(command -v do_download)" ] && do_download

	## Step 4: Build
	[ -n "$(command -v do_build)" ] && do_build

	## Step 5: Install
	[ -n "$(command -v do_install)" ] && do_install

	# Delete dir after build
	cd "$xb_parent_dir"
	# $subin is to delete all files regardless of file permission issues.
	# e.g. jgmenu has some issues clearing up files due to permission issue.
	$subin find "$builddir" -name "*" -delete

done

## Step 6: Remove build deps
remove_packages "${install_packages_build}"
